﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.Objects;
using System.Data.Objects.DataClasses;

namespace INSTA.Samples.EntityFramework.WinUI
{
    public partial class CourseViewer : Form
    {

        //Create an ObjectContext instance based on SchoolEntity
        private SchoolSampleEntities _schoolContext;

        private IEnumerable<Department> _departments;

        private Department _selectedDepartment;
        private IEnumerable<Course> _courses;

        public CourseViewer()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void CourseViewer_Load(object sender, EventArgs e)
        {
            //Initialize the ObjectContext
            _schoolContext = new SchoolSampleEntities();

            
            

            // Define a query that returns all Department  
            // objects and course objects, ordered by name.
            _departments = from department in _schoolContext.Departments.Include("Courses")
                                  orderby department.Name
                                 select department;

            

            try
            {
                // Bind the ComboBox control to the query, 
                // which is executed during data binding.
                // To prevent the query from being executed multiple times during binding, 
                // it is recommended to bind controls to the result of the Execute method. 
                this.cboDepartmentList.DisplayMember = "Name";
                this.cboDepartmentList.DataSource = ((ObjectQuery)_departments).Execute(MergeOption.AppendOnly);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void departmentList_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                // Get the object for the selected department.
                _selectedDepartment = cboDepartmentList.SelectedItem as Department;

                _courses = _selectedDepartment.Courses;

                // Bind the grid view to the collection of Course objects
                //  that are related to the selected Department object.
                courseGridView.DataSource = _selectedDepartment.Courses;

                //  Hide the columns that are bound to the navigation properties on Course.
                courseGridView.Columns["Department"].Visible = false;
                courseGridView.Columns["StudentGrades"].Visible = false;
                courseGridView.Columns["OnlineCourse"].Visible = false;
                courseGridView.Columns["OnsiteCourse"].Visible = false;
                courseGridView.Columns["People"].Visible = false;
                courseGridView.Columns["DepartmentId"].Visible = false;

                courseGridView.AllowUserToAddRows = true;
                courseGridView.AllowUserToDeleteRows = false;
                courseGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        



    }
}
